import logging
import datetime

class DebugLogger:
    """调试日志工具类"""
    
    def __init__(self, log_file=None):
        """初始化日志工具"""
        log_format = "%(asctime)s - %(levelname)s - %(message)s"
        if log_file is None:
            log_file = f"debug_{datetime.datetime.now().strftime('%Y%m%d_%H%M%S')}.log"
            
        logging.basicConfig(
            filename=log_file,
            level=logging.DEBUG,
            format=log_format
        )
        
        # 同时输出到控制台
        console = logging.StreamHandler()
        console.setLevel(logging.DEBUG)
        formatter = logging.Formatter(log_format)
        console.setFormatter(formatter)
        logging.getLogger('').addHandler(console)
        
    @staticmethod
    def log_data(message, data):
        """记录数据和消息"""
        logging.debug(f"{message}: {str(data)}")
    
    @staticmethod
    def log_error(message, exception=None):
        """记录错误"""
        if exception:
            logging.error(f"{message}: {str(exception)}")
        else:
            logging.error(message)


def inspect_data_flow(input_data, calculated_data, displayed_data=None):
    """检查数据流是否正常"""
    try:
        logger = DebugLogger()
        logger.log_data("输入数据", input_data)
        logger.log_data("计算后数据", calculated_data)
        
        if displayed_data is not None:
            logger.log_data("显示的数据", displayed_data)
            
        # 检查数据流转换是否正常
        if not calculated_data:
            logger.log_error("计算结果为空")
            
        # 检查输出数据格式是否符合表格要求
        for key, value in calculated_data.items():
            if not isinstance(key, str) or not isinstance(value, (int, float, str)):
                logger.log_error(f"数据格式不正确: {key} -> {value}")
                
    except Exception as e:
        logging.error(f"检查数据流时发生错误: {str(e)}")
